package vip.xumy.core.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

import org.junit.Test;

import vip.xumy.core.exception.CoreException;
import vip.xumy.core.utils.Base64Util;
import vip.xumy.core.utils.RSAUtil;
import vip.xumy.core.utils.StringUtil;

/**	All rights reserved
 *	author:mengyxu
 *	date:2020年6月11日
 */

public class RSAUtilTester {
	public static final String a = "138128429165014960214288316246915564109957848967973935739058724552651480736930647934382755460619033465620384391387196627089864034424350665139841418169631846827850418205510584071030219835341930960684577738773846628024223162766742868530563861053134130417499539521288428945157726371402147367583657263208271059771";
	
	//字符串进行加密填充的名称
	public static final String PADDING = "RSA/NONE/NoPadding";
	//字符串持有安全提供者的名称
	public static final String PROVIDER = "BC";
	
	@Test
	public void stringToByteKey() throws CoreException {
		String key = "0008000001000100BEC07F26C1D87D7E022D3E82A4F8A0E83E2079206D728EFF0123194DFD6627655ACA16FA16EB076A09B85902E19727B1D8CCE91F998DD5EFB5D26CCE9B4BB1A0D2708B59EFA1217305BA8838960B1862198F2847C0182D5CA8F38D95949A3AD07E576713405C82DB06F89744D80660D562C8B13BE56E048DA0EC7934550653B6AFA2BB7ACDD893E595303E826AAE62095FD8280304BF5EE00DDE4DB3D5D20BFBEFA6C7D527D87E1F6BEA415E143F400A79AC1F24077AC205E433C730977AAC85A4BA2138AAA4B64C95AE14357204A6A6F708DF60290A94358358AF416EFB97E585EBC368E0F6BB189B0368861E1F8D1C62D53A9CF4A2BC4308883B87455FF72F";
		byte[] arr = RSAUtil.StringToByteKey(key);
		for (byte b : arr) {
			System.out.println(b);
		}
	}
	
	@Test
	public void byteToStringKey() throws IOException {
		File file = new File("D:/pubkey.Rsapub");
		FileInputStream fis = new FileInputStream(file);
		byte[] arr = new byte[fis.available()];
		fis.read(arr);
		fis.close();
		String key = RSAUtil.byteToStringKey(arr);
		System.out.println(key);
	}
	
	@Test
	public void encryptByPublicKey() throws IOException {
		String key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvsB/JsHYfX4CLT6CpPig6D4geSBtco7/ASMZTf1mJ2Vayhb6FusHagm4WQLhlyex2MzpH5mN1e+10mzOm0uxoNJwi1nvoSFzBbqIOJYLGGIZjyhHwBgtXKjzjZWUmjrQfldnE0BcgtsG+JdE2AZg1WLIsTvlbgSNoOx5NFUGU7avort6zdiT5ZUwPoJqrmIJX9goAwS/XuAN3k2z1dIL+++mx9Un2H4fa+pBXhQ/QAp5rB8kB3rCBeQzxzCXeqyFpLohOKqktkyVrhQ1cgSmpvcI32ApCpQ1g1ivQW77l+WF68No4Pa7GJsDaIYeH40cYtU6nPSivEMIiDuHRV/3LwIDAQAB";
		String data = "123456789";
		String result = RSAUtil.encryptByPublicKey(data, key);
		System.out.println(result);
	}
	
	@Test
	public void dcryptByPublicKey() throws IOException {
		String key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvsB/JsHYfX4CLT6CpPig6D4geSBtco7/ASMZTf1mJ2Vayhb6FusHagm4WQLhlyex2MzpH5mN1e+10mzOm0uxoNJwi1nvoSFzBbqIOJYLGGIZjyhHwBgtXKjzjZWUmjrQfldnE0BcgtsG+JdE2AZg1WLIsTvlbgSNoOx5NFUGU7avort6zdiT5ZUwPoJqrmIJX9goAwS/XuAN3k2z1dIL+++mx9Un2H4fa+pBXhQ/QAp5rB8kB3rCBeQzxzCXeqyFpLohOKqktkyVrhQ1cgSmpvcI32ApCpQ1g1ivQW77l+WF68No4Pa7GJsDaIYeH40cYtU6nPSivEMIiDuHRV/3LwIDAQAB";
		String data = "rBmtPB1ip3Z9nEaUHt2PCaC47CLD/N+SU3CQSMYUSlujTzzw2pJorSXQiRo3nm3gmQjQiPC0JWjhyRzXw1D1TDHr0aRlFv3GuvO2YUkWivxGh4bPArOT7VLEzerjWJ4N9fqPlGi9ZgMbEDhhiONBJ9ipX8t+w2ymTPpcngHRUZQM2vNk/CMgh1F5caYqsqW28XmI5unL3FYGR0U8GK6XfQs/O8doLxXHZ1XSCymjTOMeZ8uhkY/I66pGFS5wjz+m0ZqjcTDwE9UHYGPVNfIx3sgHZ8YyIr/kYxC94dGXp19sTIQBp14QK0pwD6m7xPIQcMOsoZsCVKbqJF/CCjdfgg==";
		String result = RSAUtil.decryptByPublicKey(data, key);
		System.out.println(result);
	}
	
	@Test
	public void encryptByPublicKey1() throws Exception {
		
		File file = new File("D:/pubkey.der");
		FileInputStream fis = new FileInputStream(file);
		byte[] arr = new byte[fis.available()];
		System.out.println(arr.length);
		fis.read(arr);
		fis.close();
		byte[] arr1 = new byte[4];
		byte[] arr2 = new byte[256];
		for (int i = 0; i < 4; i++) {
			arr1[i] = arr[7-i];
		}
		for (int i = 0; i < 256; i++) {
			arr2[i] = arr[i+8];
		}
		String data = "123456789";
		KeyFactory kf = KeyFactory.getInstance("RSA");
		BigInteger modulus = new BigInteger(arr1);
		BigInteger publicExponent = new BigInteger(arr2).abs();
		System.out.println(modulus);
		System.out.println(publicExponent);
		RSAPublicKeySpec keySpec = new RSAPublicKeySpec(publicExponent, modulus);
//	    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(arr);
	    PublicKey publicKey = kf.generatePublic(keySpec);
	    Cipher cipher = Cipher.getInstance(kf.getAlgorithm());  
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);  
        byte[] result = cipher.doFinal(data.getBytes()); 
        for (byte b : result) {
			System.out.println(b);
		}
//        System.out.println(Base64Util.encode(result));
	}
	
	@Test
	public void test() throws Exception {
	    //读取pem证书
	    BufferedReader br = new BufferedReader(new FileReader("D:/pub2.pem"));
	    String s = br.readLine();
	    StringBuffer publickey = new StringBuffer();
	    while (!StringUtil.isEmpty(s)) {
	    	if(s.charAt(0) != '-')
	        publickey.append(s);
	        s = br.readLine();
	    }
	    br.close();
	    byte[] keybyte = Base64Util.decode(publickey.toString());
	    KeyFactory kf = KeyFactory.getInstance("RSA");
	    
	    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keybyte);
	    
	    PublicKey publicKey = kf.generatePublic(keySpec);
	    Cipher cipher = Cipher.getInstance(kf.getAlgorithm());  
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);  
        byte[] result = cipher.doFinal("123456789".getBytes()); 
        System.out.println(Base64Util.encode(result));
        FileOutputStream fos = new FileOutputStream(new File("D:/pub2.bin"));
        fos.write(result);
        fos.flush();
        fos.close();
	}
	

}
